package com.example.administrator.student4;

import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by 周千遇 on 2016/11/19.
 */
public class ModifyActivity extends Activity{
    SQLiteDatabase db;
    TextView tv_name;
    EditText et_grade;
    Spinner sp_term,sp_subject;
    String grade,term,subject;
    Button modify;
    int id;
    String[] arr_subject;
    String [] arr_term = {"term one","term two"};
    Myhandler myhandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detailactivity);
        uiinti();
        myhandler = new Myhandler();
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    arr_subject = WebUtil.subjectfind();
                    myhandler.sendEmptyMessage(0x111);
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        final Bundle bundle = getIntent().getExtras();
        if(bundle!=null){
            grade = bundle.getString("grade");
            term = bundle.getString("term");
            subject = bundle.getString("subject");
            tv_name.setText(bundle.getString("name"));
            et_grade.setText(grade);
            id = bundle.getInt("id");
           if(term.equals("term one")){
               sp_term.setSelection(0);
            }
           else sp_term.setSelection(1);
        }
        modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                grade = et_grade.getText().toString();
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        String path = "http://10.0.2.2:8080/andorid/grade?request=update&grade="+grade+"&id="+id;
                        try {
                            URL url = new URL(path);
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                            connection.setRequestMethod("POST");
                            connection.connect();
                            connection.getInputStream();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                Toast.makeText(ModifyActivity.this,"修改成功",Toast.LENGTH_LONG).show();
            }
        });
    }
    public void uiinti(){
        et_grade = (EditText) findViewById(R.id.grade);
        sp_term = (Spinner) findViewById(R.id.term);
        sp_subject = (Spinner) findViewById(R.id.subject);
        tv_name = (TextView) findViewById(R.id.name);
        modify = (Button) findViewById(R.id.modify);

    }
    class Myhandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what ==0x111) {
                ArrayAdapter<String> apsubject = new ArrayAdapter<String>(ModifyActivity.this,android.R.layout.simple_expandable_list_item_1,arr_subject);
                ArrayAdapter<String> apterm = new ArrayAdapter<String>(ModifyActivity.this,android.R.layout.simple_expandable_list_item_1,arr_term);
                sp_term.setAdapter(apterm);
                sp_subject.setAdapter(apsubject);
            }
        }
    }
}
